/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package cassandrawindow;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 *
 * @author quyennb
 */
public class HandleDataTableCampaignDate {

    public static final String KEYSPACE                         =   "AdsData";
    public static final String ColumFamly                       =   "BannerViewCountCf";
    public static final String ColumFamlyBannerClickCountCf     =   "BannerClickCountCf";
    public static final String ColumFamlyBannerViewCountCf      =   "BannerViewCountCf";

    public static final String ColumFamlyCampaignClickCountCf       =   "CampaignCountCf";
    public static final String ColumFamlyCampaignViewCountCf        =   "CampaignViewCountCf";

    public void handleData() throws ParseException
    {
       long first_time = System.currentTimeMillis();
        CassandraAction cassAct = new CassandraAction(ACCOUNT.CLUSTERNAME ,ACCOUNT.URL, ACCOUNT.PORT);
        cassAct.initReadWirte(KEYSPACE);
        MysqlDataManager mysqldatamanager = new MysqlDataManager();
        CqlDataManager cqldatamanager = new CqlDataManager();
        List listbanners = cqldatamanager.getAllCampainID();
        
        String currentDate = TimeUtil.getCurrentTimeInString();//2012-07-24
        StringBuilder sbff = new StringBuilder();

        Iterator it=listbanners.iterator();
        while(it.hasNext())
        {
            String key = (String)it.next();
            String str = getClickInMonth(mysqldatamanager, cassAct, currentDate, key, ColumFamlyCampaignClickCountCf, ColumFamlyCampaignViewCountCf);
            sbff.append(str);
            
            if(sbff.toString().length() > 10000)
            {
                //xu li cho du lieu cuoi
                String relStr = sbff.toString();
                String valInsert = relStr.substring(0, (relStr.length()-1));
                mysqldatamanager.insertDataCampaignDay(valInsert);
                sbff.setLength(0);
            }
        }

        if(sbff.toString().length() > 1)
        {
            //xu li cho du lieu cuoi
            String relStr = sbff.toString();
            String valInsert = relStr.substring(0, (relStr.length()-1));
            mysqldatamanager.insertDataCampaignDay(valInsert);
            sbff.setLength(0);
        }

        long last_time = System.currentTimeMillis();
        System.out.println("Thoi gian chay  :" +(last_time-first_time));
        Debug.println("success HandleDataTableCampaignDate");

    }

     public static String getClickInMonth(MysqlDataManager mysqldatamanager,CassandraAction cassAct,String currentDay ,String key,String columnFamilyClick,String columnFamilyView) throws ParseException{

        StringBuilder sb = new StringBuilder();
        String changeDay = currentDay.split("-")[2]+ "/" + currentDay.split("-")[1]+"/" + currentDay.split("-")[0];
        String date = "00:00:00 " + changeDay ;
        long start_time = TimeUtil.longToDateWithHour(date);
        //them 1 ngay
        long finish_time = start_time + 24*60*60*1000;
        long countClick = cassAct.getCountClickByIntervalTime(key, columnFamilyClick, start_time, finish_time);
        long countView = cassAct.getCountClickByIntervalTime(key, columnFamilyView, start_time, finish_time);        
        if((countView >0))
        {
            String str = "(" + key + "," + countClick + "," + countView +",0,0,0,0,0, '" + currentDay + "',0,0,0)";
            sb.append(str).append(",");
        }
        //mysqldatamanager.insertDataCampaignDay(key, String.valueOf(countClick), String.valueOf(countView), (prefixDB +i));        
        return sb.toString();
        
    }        
}
